<?php
class Vs_User
    extends Vs_Entity
{
    const TYPE_WEB = 7;
    const TYPE_EDITOR = 5;
    const TYPE_ADMIN = 3;
    const TYPE_FULLADMIN = 1;
    
    const SEX_MALE = 1;
    const SEX_FEMALE = 2;
    
    public static $types = array(
        self::TYPE_FULLADMIN => 'fulladmin',
        self::TYPE_ADMIN => 'admin',
        self::TYPE_EDITOR => 'editor',
        self::TYPE_WEB => 'web'
    );
    
    public static $sexs = array(
        self::SEX_MALE => 'male',
        self::SEX_FEMALE => 'female'
    );
    
    public $userId;
    public $email;
    public $name;
    public $surname;
    public $birthdate;
    public $sex;
    public $cityId;
    public $type;
    public $password;
    public $code;
    public $active;
    
    
    public function getId()
    {
        return $this->userId;
    }
    
    public function setId($userId)
    {
        $this->userId = $userId;
    }
    
    public function getType()
    {
        return self::$types[$this->type];
    }
    
	public function __toString()
    {
        return $this->name;
    }
    
    
    /**
     * @param code: codigo de activacion
     * @return bool
     */
    public function activate( $code )
    {
    	if( $this->isActive() ){
    		throw new Vs_Exception( Vs_Exception::USER_ALREADY_ACTIVE );
    	}
    
    	if(  $this->code != $code ){
    		throw new Vs_Exception( Vs_Exception::USER_INVALID_ACTIVATION_CODE );
    	}
    		
    	$this->active = 1;
    	$this->code = null;
    }
    
    
    /**
     * @return bool
     */
    public function isActive()
    {
    	return (bool) $this->active;
    }
    
    /**
     * Genera un codigo random de 10 caracteres
     * @return string
     */
    public function generateCode( )
    {
    	$this->code = Vs_Encryptor::code();
    }
    
    /**
     * Genera el codigo interno para resetear la clave
     * @return $this
     */
    public function forgotPassword ()
    {
    	$this->generateCode();
    	return $this;
    }
    
    /**
     * @param string password
     * @param string code
     * @return $this
     */
    public function changePassword ( $password, $code )
    {
    	if(  $this->code != $code ){
    		throw new Vs_Exception( Vs_Exception::USER_INVALID_CODE );
    	}
    		
    	$this->active = 1;
    	$this->code = null;
    	$this->setPassword($password);
    
    	return $this;
    }
    
    /**
     * @param string $password
     * @return $this
     */
    public function setPassword( $password )
    {
    	$this->password  = Vs_Encryptor::password( $password );
    	return $this;
    }
    
    public function isFacebookUser()
    {
    	return ! empty( $this->fbUserId );
    }
    
    
    
}